//
//  WYEditMessageController.m
//  Boss
//
//  Created by admin on 2018/7/3.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYEditMessageController.h"
#import "WYVIPController.h"
@interface WYEditMessageController ()<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topHeight;
@property (strong, nonatomic) IBOutlet UITextField *textField;

@end

@implementation WYEditMessageController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
-(void)setupUI
{
    self.topHeight.constant = self.navHeight;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:(UIBarButtonItemStyleDone) target:self action:@selector(cancelAction:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:(UIBarButtonItemStyleDone) target:self action:@selector(saveAction:)];
    self.navigationItem.title = self.navTitle;
    self.textField.placeholder = self.placeHoder;
    self.view.backgroundColor =[UIColor hexStringToColor:@"#e6e6e6" alpha:1.0];
    if (self.textFieldText.length) {
        self.textField.text = self.textFieldText;
    }
    self.textField.delegate = self;
    
    [self.textField addTarget:self action:@selector(textDidChangeWithSix:) forControlEvents:(UIControlEventEditingChanged)];
    
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    BOOL isDeleting = range.length > 0 && string.length <= 0;
    if (isDeleting && range.location + range.length > textField.text.length) {
        return NO;
    }
    return YES;
    
}


-(void)textDidChangeWithSix:(UITextField *)textField
{
    NSString *lang = [[UITextInputMode currentInputMode]primaryLanguage];//键盘输入模式
    
    if ([lang isEqualToString:@"zh-Hans"]) {// 简体中文输入，包括简体拼音，健体五笔，简体手写
        
        UITextRange *selectedRange = [textField markedTextRange];
        
        //获取高亮部分
        
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        
        //没有高亮选择的字，则对已输入的文字进行字数统计和限制
        
        if (!position) {
            
            if (textField.text.length >6) {
                
                textField.text = [textField.text substringToIndex:6];
                
            }
            
        }
        
        //有高亮选择的字符串，则暂不对文字进行统计和限制
        
        else{
            
            
            
        }
        
    }
    
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    
    else{
        
        if (textField.text.length >6) {
            
            textField.text = [textField.text substringToIndex:6];
            
        }
        
    }
    
}


//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    if (textField == self.textField)
//    {
//        if (string.length == 0) return YES;
//
//        NSInteger existedLength = textField.text.length;
//        NSInteger selectedLength = range.length;
//        NSInteger replaceLength = string.length;
//        if (existedLength - selectedLength + replaceLength > 6)
//        {
//            return NO;
//        }
//    }
//
//    return YES;
//}





//-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//
//    if (textField == self.textField) {
//        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
//        if (range.length == 1 && string.length == 0) {
//            return YES;
//        }
//        //so easy
//        else if (self.textField.text.length >= 6) {
//            self.textField.text = [textField.text substringToIndex:6];
//            return NO;
//        }
//    }
//    return YES;
//
//
//
//
//
//}




-(void)cancelAction:(UIBarButtonItem *)item
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)saveAction:(UIBarButtonItem *)item
{
//    if ([self.textField.text isEqualToString:@"12138"]) {
//
//        WYVIPController *vip = [[WYVIPController alloc]init];
//        [self.navigationController pushViewController:vip animated:YES];
//        return;
//
//    }
    
    
    
    
    
    if (!self.textField.text.length) {
        [self toast:self.placeHoder];
        return;
    }
    
    WEAKSELF
    if (self.tag==1) {
        [WYHttpServer editCompanyNameWithCompanyName:self.textField.text SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
            if (success) {
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }else{
                [weakSelf toast:errorMessage];
            }
        }];
    }
    if (self.tag==2) {
        [WYHttpServer editdutyWithCompanyduty:self.textField.text SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
            if (success) {
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }else{
                [weakSelf toast:errorMessage];
            }
        }];
    }
    
    if (self.tag==3) {
        
        
    [WYHttpServer editUserMessageWithHeadImgUrl:nil WithUserName:self.textField.text WithSex:nil WithBirthday:nil WithProvince:nil WithCity:nil WithPrefecture:nil SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        
            if (success) {
                    [weakSelf.navigationController popViewControllerAnimated:YES];
            }else{
                [weakSelf toast:errorMessage];
            }
     }];
        
        
        
        
        
        
        
        
    }
    
    
    

    
    
    
    
}
@end
